<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/13 0013
 * Time: 10:30
 */

namespace logic\jihuo\driver;

use logic\LogicException;
use mod\User;
use mod\Zuhe;
use mod\Pihao;
use mod\Lianpiao;


class Base
{
	//出错提示信息 根据source不同提示不同内容
	public $msg = array();
	//数据信息 如果是激活操作来自表单 如果是repare操作来源lianpiao表
	public $data = array();
	//操作来源 pc/mobile/admin
	public $source = "";
	//来源显示信息 2.0网站前台 微信公众平台 2.0网站后台
	public $sourceMsg = '';
	//lianpiao表查询结果集
	public $lianpiaoResult = array();
	//操作用户信息
	public $userInfo = array();
	//zuhe表查询结果集
	public $zuheResult = array();
	//pihao表查询结果集
	public $pihaoResult = array();
	//绑定账号的uid
	public $bindUserId = 0;
	public $lianpiaoModel;

	/**
	 * 析构函数
	 * Jihuo constructor.
	 */
	public function __construct()
	{
	}

	/**
	 * 设置来源接口
	 * @param $source
	 * 'pc' => '2.0网站前台',
	 * 'mobile' => '微信公众平台',
	 * 'admin' => '2.0网站后台',
	 */
	public function setSource($source)
	{
		$this->source = $source;
		$this->setMsg();
	}

	/**
	 * 设置数据信息接口
	 * @param array $data
	 */
	public function setData($data = array())
	{
		$this->data = $data;
		$this->data ['shenfenid'] = !isset($this->data ['shenfenid']) ? '' : strtoupper( $this->data ['shenfenid'] );
	}


	/**
	 * 操作用户信息接口
	 * @param $userInfo
	 */
	public function setUserInfo($userInfo)
	{
		$this->userInfo = $userInfo;
	}

	/**
	 * 设置出错提示信息接口 根据source
	 */
	public function setMsg()
	{
		$errorMsg = [
			'pc' => [
				'shenfenid' => '请输入正确的身份号！',
				'is_black' => '本产品禁止旅行社和旅游群团队使用，此注册信息已被系统锁定，无法使用联票,请与销售方联系!',
				'empty_lianpiaono' => '没有该联票！',
				'iskaitong' => '该联票未开通！',
				'issuo' => '该联票已被冻结！',  //联票锁定
				'isjihuo' => '该联票已激活！',
				'is_suoding' => '该批号锁定中请开通后再进行激活',   //批号锁定
				'phone_activation_num' => '当前手机号激活 (联票) 次数已超过###phone_activation_num###次,请更换手机号码！',  //普通联票手机激活数量
				'under_age' => '此联票只能由###is_shenfenzheng###岁及以下人激活使用！',
				'up_age' => '此联票只能由###is_shenfenzheng###岁及以上人激活使用！',
				'error_age' => '参数错误',
				'repeat_shenfenid' => '当前身份证号已经激活过此批号！',
				'expire_time' => '此联票已过激活期！',
				'save_lianpiao' => '更新预定人信息失败，请稍后重试！',
				'error_new_account' => '写入新用户出错，请稍后重试！',
			],
			'mobile' => [
				'shenfenid' => '请输入正确的身份号！',
				'is_black' => '本产品禁止旅行社和旅游群团队使用，此注册信息已被系统锁定，无法使用联票,请与销售方联系!',
				'empty_lianpiaono' => '没有该联票！',
				'iskaitong' => '该联票未开通！',
				'issuo' => '该联票已被冻结！',  //联票锁定
				'isjihuo' => '该联票已激活！',
				'is_suoding' => '该批号锁定中请开通后再进行激活',   //批号锁定
				'phone_activation_num' => '当前手机号激活 (联票) 次数已超过###phone_activation_num###次,请更换手机号码！',  //普通联票手机激活数量
				'under_age' => '此联票只能由###is_shenfenzheng###岁及以下人激活使用！',
				'up_age' => '此联票只能由###is_shenfenzheng###岁及以上人激活使用！',
				'error_age' => '参数错误',
				'repeat_shenfenid' => '当前身份证号已经激活过此批号！',
				'expire_time' => '此联票已过激活期！',
				'save_lianpiao' => '更新预定人信息失败，请稍后重试！',
				'error_new_account' => '写入新用户出错，请稍后重试！',
			],
			'admin' => [
				'shenfenid' => '请输入正确的身份号！',
				'is_black' => '"本产品违规使用，注册信息已加黑',
				'empty_lianpiaono' => '没有该联票！',
				'iskaitong' => '该联票未开通！',
				'issuo' => '该联票已被冻结！',  //联票锁定
				'isjihuo' => '该联票已激活！',
				'is_suoding' => '该批号锁定中请开通后再进行激活',   //批号锁定
				'phone_activation_num' => '当前手机号激活 (联票) 次数已超过###phone_activation_num###次,请更换手机号码！',  //普通联票手机激活数量
				'under_age' => '此联票只能由###is_shenfenzheng###岁及以下人激活使用！',
				'up_age' => '此联票只能由###is_shenfenzheng###岁及以上人激活使用！',
				'error_age' => '参数错误',
				'repeat_shenfenid' => '当前身份证号已经激活过此批号！',
				'expire_time' => '此联票已过激活期！',
				'save_lianpiao' => '更新预定人信息失败，系统异常，请联系管理员！',
				'error_new_account' => '写入新用户出错，系统异常，请联系管理员！',
			],
		];
		$this->msg = $errorMsg[$this->source];

		$sourceMsg = [
			'pc' => '2.0网站前台',
			'mobile' => '微信公众平台',
			'admin' => '2.0网站后台',
		];
		$this->sourceMsg = $sourceMsg[$this->source];
	}

	/*
	 * 获取联票信息
	 */
	public function getLianpiaoResult(){
		isset($this->data['id'])  && $where = array('id'=> $this->data['id']);
		isset($this->data['lianpiaono'])  && $where = array('lianpiaono'=> $this->data['lianpiaono']);
		$this->lianpiaoModel = new Lianpiao();
		$this->lianpiaoResult = $this->lianpiaoModel->where ($where)->find();
		if (empty($this->lianpiaoResult)) {
			throw new LogicException($this->msg['empty_lianpiaono']);
		}
	}

	/*
	 * 获取组合信息
	 */
	public function getZuheResult(){
		empty($this->lianpiaoResult) && $this->getLianpiaoResult();
		$zuheModel = new Zuhe();
		$this->zuheResult = $zuheModel->where("id", $this->lianpiaoResult['zuhe_id'])->find();
	}

	/**
	 * 数据检验 身份证、手机号检验
	 * 判断身份证、手机号是否在黑名单
	 * @throws Exception
	 */
	public function checkBlack()
	{
		$black_seach = db('blacklist')->where("black", $this->data['phone'])->whereOr("black", $this->data['shenfenid'])->find();
		if(!empty($black_seach)) {
			throw new LogicException($this->msg['is_black']);
		}
	}

	public function checkLianpiaoIsjihuo(){
		empty($this->lianpiaoResult)  && $this->getLianpiaoResult();

		if ($this->lianpiaoResult['isjihuo'] == 10) {
			throw new LogicException($this->msg['isjihuo']);
		}
	}

	/**
	 * 数据检验 联票信息检验
	 * 根据联票号获取联票全部信息
	 * @throws Exception
	 */
	public function checkLianpiao()
	{
		empty($this->lianpiaoResult)  && $this->getLianpiaoResult();

		if ($this->lianpiaoResult['iskaitong'] != 10) {
			throw new LogicException($this->msg['iskaitong']);
		}
		if ($this->lianpiaoResult['issuo'] == 10) {
			throw new LogicException($this->msg['issuo']);
		}
		if($this->lianpiaoResult['isjihuo'] == 1){
			$jihuotime = strtotime($this->lianpiaoResult['jihuodate']);
			$nowtime = strtotime ( date ( 'Y-m-d' ) );
			if ($jihuotime < $nowtime) {
				throw new LogicException($this->msg['expire_time']);
			}
		}
	}

	/*
	 * 检查身份证号码在同批号中出现的数量
	 */
	public function checkCardCount($flag = true){
		empty($this->lianpiaoResult)  && $this->getLianpiaoResult();

		//验证同批号产品中身份证唯一
		$sfzhcount = Lianpiao::where ("pihao_id", $this->lianpiaoResult['pihao_id'])->where("shenfenid", $this->data['shenfenid'])->count();
		if ($flag && $sfzhcount >= 1 or !$flag && $sfzhcount > 1) {
			throw new LogicException($this->msg['repeat_shenfenid']);
		}

	}

	/**
	 * 数据检验 批号是否激活检验
	 * 判断批号是否激活
	 * @throws Exception
	 */
	public function checkPihao()
	{
		$isok = Pihao::get($this->lianpiaoResult['pihao_id'])->toArray();
		if($isok['is_suoding']==0) {
			throw new LogicException($this->msg['is_suoding']);
		}
		//自激活之日起使用截止日期增加固定天数
		if($isok['yxq']>0){
			$this->data['shiyongdate']=date("Y-m-d",strtotime("+".$isok['yxq']." day"));
			if($this->data['shiyongdate'] > $this->lianpiaoResult['shiyongdate']){
				$this->data['shiyongdate'] = $this->lianpiaoResult['shiyongdate'];
			}
		}
		!isset($this->data['shiyongdate']) && $this->data['shiyongdate'] = $this->lianpiaoResult['shiyongdate'];
	}

	/*
	 * 同一手机号码激活票号数量
	 * @param flag bool    value=>true  激活操作   value=>false 修改操作
	 */
	public function phoneActivationNum($flag = true){
		empty($this->zuheResult) && $this->getZuheResult();

		if($this->zuheResult['phone_activation_num'] == 0) return ;
		$cnts = $this->lianpiaoModel
			->where('phone',$this->data[ 'phone' ])
			->where('pihao_id',$this->lianpiaoResult['pihao_id'])
			->field('id')->count();

		if($flag && $this->zuheResult['phone_activation_num'] <= $cnts ){
			throw new LogicException( str_replace('###phone_activation_num###',$this->zuheResult['phone_activation_num'],$this->msg['phone_activation_num']) );
		}

		/*
		 *票号信息修改
		 */
		if(!$flag){
			if($this->data['phone'] == $this->lianpiaoResult['phone']){
				if( $this->zuheResult['phone_activation_num'] < $cnts ){
					throw new LogicException( str_replace('###phone_activation_num###',$this->zuheResult['phone_activation_num'],$this->msg['phone_activation_num']) );
				}
			}else{
				if( $this->zuheResult['phone_activation_num'] <= $cnts ){
					throw new LogicException( str_replace('###phone_activation_num###',$this->zuheResult['phone_activation_num'],$this->msg['phone_activation_num']) );
				}
			}
		}
	}




	/**
	 * 数据检验 用户信息检验
	 * @return int|mixed|string
	 * @throws Exception
	 */
	public function checkUser()
	{
		$user = array();
		$user['phone'] = $this->data['phone'];
		$user['code'] = random();
		$user['nickname']=trim($this->data['username']);
		$user['password']=md5(substr($this->data ['shenfenid'],-6).$user['code']);
		$user['credits']='100';
		$user['rid']='5';
		$user['username'] = username();
		$user['regtime']=time();
		$user['weixin'] = '0';

		$userModel = new User();
		$userResult = $userModel->where(['phone'=>$this->data['phone']])->find();
		if(empty($userResult)){
			//生成会员账号
			$userResult = $userModel->userReg($user,false,false);
			if(!$userResult) throw new LogicException($this->msg['error_new_account']);
			$bangdingrenid = $userResult;
		}else{
			$bangdingrenid = $userResult->uid;
		}
		return $bangdingrenid;
	}

	public function bindUser(){
		//创建绑定人账号uid
		$this->bindUserId = $this->source == 'admin' ? $this->checkUser() : $this->userInfo['uid'];
	}

}